package com.paic.mhis.hcpms.agreement.healthcareprovider.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.HealthCareService;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.ValidatorUtil;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.web.BaseRest;

@Controller
public class HealthCareController extends BaseRest {

	@BizServicesProxy
	private HealthCareService healthCareService;

	public void setHealthCareService(HealthCareService healthCareService) {
		this.healthCareService = healthCareService;
	}

	public HealthCareService getHealthCareService() {
		return healthCareService;
	}

	/**
	 * 医疗机构基本信息新增
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/base/create", method = RequestMethod.POST)
	public Map<String, Object> addProvider(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJson(request, transferJson);
			result = this.healthCareService.addProvider(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医疗机构基本信息修改
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/base/update", method = RequestMethod.POST)
	public Map<String, Object> updateProvider(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJsonUpdate(request, transferJson);
			result = this.healthCareService.updateProvider(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医疗机构基本信息查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/base/show")
	public Map<String, Object> queryProvider(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String providerid = request.getParameter("id");
			String name = request.getParameter("name");
			JSONObject transferJson = new JSONObject();
			transferJson.put("providerid", providerid);
			transferJson.put("name", name);
			result = this.healthCareService.queryProvider(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医疗机构列表查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/base/page", method = RequestMethod.GET)
	public Map<String, Object> queryProviderList(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String name = request.getParameter("name");
			String license = request.getParameter("license");
			String orgcode = request.getParameter("orgcode");
			String page = request.getParameter("page");
			String pageSize = request.getParameter("pageSize");
			if (!isEmpty(page) || !isEmpty(pageSize)) {
				if (!ValidatorUtil.isPlusInteger(page)
						|| !ValidatorUtil.isPlusInteger(pageSize)) {
					result = new HashMap<String, Object>();
					HttpProcessUtil.generateExceptionResult(result,
							Constants.STATE_KEY,
							Constants.FAILED_VALIDATE_STATE_VALUE,
							Constants.MSG_KEY,
							Constants.PAGE_VALIDATE_FAILED_MSG);
					return result;
				}
			}
			JSONObject transferJson = new JSONObject();
			transferJson.put("name", name);
			transferJson.put("license", license);
			transferJson.put("orgcode", orgcode);
			transferJson.put("page", page);
			transferJson.put("pageSize", pageSize);
			result = this.healthCareService.queryProviderList(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医疗机构列表删除
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/base/delete", method = RequestMethod.POST)
	public Map<String, Object> deleteProvider(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {

			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJsonUpdate(request, transferJson);
			result = this.healthCareService.deleteProvider(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	private boolean isEmpty(String param) {
		return param == null || param.equals("");
	}
}
